package com.njd.test.entity;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.TableMapping;
import com.jfinal.plugin.activerecord.Db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

/***
 * 自动生成的Model
 * 
 * @author Johnson Zhang
 *
 */
@SuppressWarnings("serial")
public class Data extends Model<Data> {
	// --------------setter和getter---------------

	public Data setId(Integer id) {
		set("id", id);
		return this;
	}

	public Integer getId() {
		return get("id");
	}

	public Data setModuleid(Integer moduleid) {
		set("moduleId", moduleid);
		return this;
	}

	public Integer getModuleid() {
		return get("moduleId");
	}

	public Data setRemark(String remark) {
		set("remark", remark);
		return this;
	}

	public String getRemark() {
		return get("remark");
	}

	public Data setModulename(String modulename) {
		set("moduleName", modulename);
		return this;
	}

	public String getModulename() {
		return get("moduleName");
	}

	public Data setConcurrentuser(String concurrentuser) {
		set("concurrentUser", concurrentuser);
		return this;
	}

	public String getConcurrentuser() {
		return get("concurrentUser");
	}

	public Data setProjectid(Integer projectid) {
		set("projectId", projectid);
		return this;
	}

	public Integer getProjectid() {
		return get("projectId");
	}

	public Data setErrorreponse(String errorreponse) {
		set("errorReponse", errorreponse);
		return this;
	}

	public String getErrorreponse() {
		return get("errorReponse");
	}

	public Data setSuccessreponse(String successreponse) {
		set("successReponse", successreponse);
		return this;
	}

	public String getSuccessreponse() {
		return get("successReponse");
	}

	public Data setDate(Date date) {
		set("date", date);
		return this;
	}

	public Date getDate() {
		return get("date");
	}

	public Data setProjectname(String projectname) {
		set("projectName", projectname);
		return this;
	}

	public String getProjectname() {
		return get("projectName");
	}

	public Data setSuccessrate(String successrate) {
		set("successRate", successrate);
		return this;
	}

	public String getSuccessrate() {
		return get("successRate");
	}

	public Data setResponsetime(String responsetime) {
		set("responseTime", responsetime);
		return this;
	}

	public String getResponsetime() {
		return get("responseTime");
	}

	public Data setTps(String tps) {
		set("tps", tps);
		return this;
	}

	public String getTps() {
		return get("tps");
	}

	// -------------------------------------------
	/***
	 * 数据库操作实体
	 */
	public static final Data dao = new Data();
	/**
	 * 表名
	 */
	public static final String tableName = "data";
	/**
	 * 表主键
	 */
	public static final String primaryKey[] = TableMapping.me()
			.getTable(dao.getClass()).getPrimaryKey();

	/***
	 * 空构造
	 */
	public Data() {
	}

	/***
	 * 有参构造
	 */
	public Data(Integer id, Integer moduleid, String remark, String modulename,
			String concurrentuser, Integer projectid, String errorreponse,
			String successreponse, Date date, String projectname,
			String successrate, String responsetime, String tps) {
		setData(id, moduleid, remark, modulename, concurrentuser, projectid,
				errorreponse, successreponse, date, projectname, successrate,
				responsetime, tps);
	}

	/***
	 * 数据批量设置
	 */
	public Data setData(Integer id, Integer moduleid, String remark,
			String modulename, String concurrentuser, Integer projectid,
			String errorreponse, String successreponse, Date date,
			String projectname, String successrate, String responsetime,
			String tps) {
		set("id", id);
		set("moduleId", moduleid);
		set("remark", remark);
		set("moduleName", modulename);
		set("concurrentUser", concurrentuser);
		set("projectId", projectid);
		set("errorReponse", errorreponse);
		set("successReponse", successreponse);
		set("date", date);
		set("projectName", projectname);
		set("successRate", successrate);
		set("responseTime", responsetime);
		set("tps", tps);
		return this;
	}

	/***
	 * 按ID逆序查询全部数据
	 * 
	 * @param pageNumber
	 *            页码
	 * @param pageSize
	 *            每页记录数
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> paginate(int pageNumber, int pageSize,
			String para, Object value) throws Exception {
		Object[] o = new Object[] {};
		StringBuffer sb = new StringBuffer();
		sb.append("from " + tableName);
		if (value != null && value != null && value != "") {
			o = new Object[] { value };
			sb.append(" where " + para + "=?");
		}
		sb.append(" order by " + primaryKey[0] + " DESC");
		Long num = Db.queryLong("select count(1) " + sb.toString(), o);
		List<?> data = paginate(pageNumber, pageSize, "select *",
				sb.toString(), o).getList();
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("data", data);
		m.put("num", num);
		return m;
	}

	/**
	 * 按日期查询 列表
	 * 
	 * @param date
	 * @return
	 */
	public List<Data> list(String date, int projectId) {
		return find("SELECT * FROM " + tableName + " WHERE date = ? AND projectId = ?", date, projectId);
	}

	/**
	 * 获取相近的日期
	 * 
	 * @param date
	 * @return
	 */
	public List<String> getNearDate(String date, int projectId) {
		List<String> dateList = new ArrayList<String>();
		List<String> dateListTmp = new ArrayList<String>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		List<Data> less = find(
				"SELECT date FROM "
						+ tableName
						+ " WHERE date < ? AND projectId = ? GROUP BY date ORDER BY date DESC LIMIT 5",
				date, projectId);

		for (Data d : less) {
			dateListTmp.add(df.format(d.getDate()));
		}
		for (int i = dateListTmp.size() - 1; i >= 0; i--) {
			dateList.add(dateListTmp.get(i));
		}

		int cntDate = findFirst("SELECT COUNT(0) cnt FROM " + tableName + " WHERE date = ?", date).getLong("cnt").intValue();
		if (cntDate > 0) { 
			dateList.add(date);
		}

		return dateList;
	}

	public List<String> getProjectName() {
		List<Data> project = find("SELECT projectName FROM " + tableName
				+ " GROUP BY " + R.data.projectName);

		List<String> projects = new ArrayList<String>();

		for (Data d : project) {
			projects.add(d.getProjectname());
		}

		return projects;
	}

	public List<Data> getListByDate(List<String> dateList, int projectId) {
		String whereStr = "";
		List<String> paramsTemp = new ArrayList<String>();
		for (String s : dateList) {
			whereStr += " (date = ?) OR ";
			paramsTemp.add(s);
		}

		paramsTemp.add(String.valueOf(projectId));
		Object[] arr = (Object[]) paramsTemp.toArray(new String[paramsTemp
				.size()]);
		

		List<Data> dataList = null;
		if (StrKit.notBlank(whereStr)) {
			dataList = find(
					"SELECT * FROM "
							+ tableName
							+ " WHERE ("
							+ whereStr.substring(0, whereStr.length() - 3)
							+ ") AND (projectId = ?) ORDER BY projectId, moduleId, concurrentUser ",
					arr);
		}

		return dataList;
	}
	
	/**
	 * 更新 冗余字段
	 * @param name
	 * @param id
	 * @param type
	 */
	public void updateModuleOrProjectName(String name, int id, String type) {
		if (type.equals("project")) {
			Db.update("UPDATE " + tableName + " SET projectName = ? WHERE projectId = ?", name, id);
		} else if (type.equals("module")) {
			Db.update("UPDATE " + tableName + " SET moduleName = ? WHERE moduleId = ?", name, id);
		}
	}

}